package com.example.book.in.controller;

import com.example.book.base.BaseController;
import com.example.book.base.Result;
import com.example.book.in.entity.BookCatalog;
import com.example.book.in.entity.BookInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.example.book.in.entity.BookContent;
import com.example.book.in.service.BookContentService;


/**
 * @author hcj
 * @date 2019-10-27 22:24:12
 */
@Controller
@RequestMapping(value = "/sys/in/bookContent")
public class BookContentController extends BaseController {

    @Autowired
    private BookContentService bookContentService;

    @ModelAttribute
    public BookContent get(@RequestParam(required = false) String bookCatalogId) {
        if (StringUtils.isNotBlank(bookCatalogId)) {
            BookContent content = bookContentService.getByCatalogId(bookCatalogId);
            if (content == null) {
                return new BookContent();
            }
            return content;
        } else {
            return new BookContent();
        }
    }

    /**
     * 新增修改页面
     *
     * @param content
     * @param model
     * @return
     */
    @GetMapping("/form")
    public String form(BookContent content, Model model) {
        if (content != null) {
            model.addAttribute("content", content);
        }
        return "in/content/content_form";
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public BookContent getById(BookContent bookContent) {
        return bookContent;
    }

    @RequestMapping(value = "/list")
    public String list(BookContent bookContent) {
        return "bookContent";
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public List<BookContent> info(BookContent bookContent) {
        return bookContentService.findList(bookContent);
    }


    @RequestMapping(value = "/findPage")
    @ResponseBody
    public PageInfo<BookContent> findPage(BookContent bookContent) {
        return bookContentService.findPage(bookContent);
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(BookContent bookContent) {
        bookContentService.save(bookContent);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/logicalDeletion", method = RequestMethod.POST)
    @ResponseBody
    public Result logicalDeletion(@RequestParam("id") String id) {
        bookContentService.logicalDeletion(id);
        return Result.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("ids") String[] ids) {
        bookContentService.delete(ids);
        return Result.success();
    }

}